package com.csq.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.common.util.CookieUtil;
import com.csq.sso.service.LoginService;

@Controller
@RequestMapping("/sso")
public class LoginContorller {

	@Autowired
	private LoginService loginService;

	@Value("${TOKEN_KEY}")
	private String TOKEN_KEY;

	@RequestMapping(value = "/account/login", method = RequestMethod.POST)
	@ResponseBody
	public ReturnResult accountLogin(@RequestParam String username,
			@RequestParam String password, HttpServletRequest request,
			HttpServletResponse response) {

		if(username == null || password== null){
			return ReturnResult.build(StatusCode.NULLUSERORPASSWORD, "用戶名或密碼為空!!");
		}
		
		ReturnResult result = loginService.accountLogin(username, password);
		if (result.getState() == StatusCode.SUCCESS) {
			CookieUtil.setCookie(response, username, password);
		}

		// System.out.println(result.getState());
		return result;
	}
	
	@RequestMapping(value = "/account/checkEmail", method = RequestMethod.POST)
	@ResponseBody
	public ReturnResult checkAccountEmail(@RequestParam String email,
			 HttpServletRequest request,
			HttpServletResponse response) {

		if(email == null){
			return ReturnResult.build(StatusCode.NULLUSERORPASSWORD, "用戶名或密碼為空!!");
		}
		
		ReturnResult result = loginService.checkAccountEmail(email);

		 System.out.println(result);
		return result;
	}
	
	@RequestMapping(value = "/company/checkEmail", method = RequestMethod.POST)
	@ResponseBody
	public ReturnResult checkCompanyEmail(@RequestParam String email,
			 HttpServletRequest request,
			HttpServletResponse response) {

		if(email == null){
			return ReturnResult.build(StatusCode.NULLUSERORPASSWORD, "用戶名或密碼為空!!");
		}
		
		ReturnResult result = loginService.checkCompanyEmail(email);

		 System.out.println(result);
		return result;
	}

	@RequestMapping(value = "/account/logout")
	@ResponseBody
	public ReturnResult accountLogout(@RequestParam String username,
			HttpServletRequest request, HttpServletResponse response) {

		ReturnResult result = loginService.logout(username);

		return result;
	}

	@RequestMapping(value = "/company/login", method = RequestMethod.POST)
	@ResponseBody
	public ReturnResult companyLogin(String username, String password,
			HttpServletRequest request, HttpServletResponse response) {

		if(username == null || password== null){
			return ReturnResult.build(StatusCode.NULLUSERORPASSWORD, "用戶名或密碼為空!!");
		}
		
		ReturnResult result = loginService.companyLogin(username, password);
		if (result.getState() == StatusCode.SUCCESS) {
			CookieUtil.setCookie(response, username, password);
		}

		// System.out.println(result.getState());
		return result;
	}
	
	
	@RequestMapping(value = "/company/logout")
	@ResponseBody
	public ReturnResult companyLogout(@RequestParam String username,
			HttpServletRequest request, HttpServletResponse response) {

		ReturnResult result = loginService.logout(username);

		return result;
	}

	@RequestMapping(value = "/token/{token}")
	@ResponseBody
	public String getUserToken(@PathVariable String token, String callback) {

		return "register";
	}
	
	
}
